package cn.ffcs.util.ftp;

import java.awt.event.ActionEvent;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

import sun.net.TelnetInputStream;
import sun.net.TelnetOutputStream;
import sun.net.ftp.FtpClient;

public class FtpUtil
{

    private static final String server = "134.128.35.138";
    private static final String user = "poc";
    private static final String password = "poc";
    private static final String path = "/home/ftp";

     /**
      * 
      * 获取ftp所有文件
      * @param e
      */
    public void ftpList_actionPerformed(ActionEvent e)
    {
        try
        {
            FtpClient ftpClient = new FtpClient();
            // 创建FtpClient对象
            ftpClient.openServer(server);

            // 连接FTP服务器
            ftpClient.login(user, password);
            if (path.length() != 0) ftpClient.cd(path);
            TelnetInputStream is = ftpClient.list();
            int c;
            while ((c = is.read()) != -1)
            {
                System.out.print((char) c);
            }
            is.close();
            ftpClient.closeServer();// 退出FTP服务器
        }
        catch (IOException ex)
        {
            ex.printStackTrace();
        }
    }
    
    /**
     * 
     * 文件上传
     * @param e
     */
    void putButton_actionPerformed(ActionEvent e)
    {
        String filename = "d://1.txt";
        try
        {
            FtpClient ftpClient = new FtpClient();
            ftpClient.openServer(server);
            ftpClient.login(user, password);
            if (path.length() != 0) ftpClient.cd(path);
            ftpClient.binary();
            TelnetOutputStream os = ftpClient.put(filename);
            File file_in = new File(filename);
            FileInputStream is = new FileInputStream(file_in);
            byte[] bytes = new byte[1024];
            int c;
            while ((c = is.read(bytes)) != -1)
            {
                os.write(bytes, 0, c);
            }
            is.close();
            os.close();
            ftpClient.closeServer();
        }
        catch (IOException ex)
        {
            ;
        }
    }
    
   /**
    * 
    * 下载
    * @param e
    */
   void getButton_actionPerformed(ActionEvent e)
    {
        String filename = "1.txt";
        try
        {
            FtpClient ftpClient = new FtpClient();
            ftpClient.openServer(server);
            ftpClient.login(user, password);
            if (path.length() != 0) ftpClient.cd(path);
            ftpClient.binary();
            TelnetInputStream is = ftpClient.get(filename);
            File file_out = new File(filename);
            FileOutputStream os = new FileOutputStream(file_out);
            byte[] bytes = new byte[1024];
            int c;
            while ((c = is.read(bytes)) != -1)
            {
                os.write(bytes, 0, c);
            }
            is.close();
            os.close();
            ftpClient.closeServer();
        }
        catch (IOException ex)
        {
            ;
        }
    }
    
}
